import isEmpty from 'lodash/isEmpty'

/**
 * 获取 用户数据
 */
export function getUser() {
	let result = uni.getStorageSync('user') || {}
	if (isEmpty(result)) {
		result.nickName = '未登陆'
		result.avatarUrl = '/static/images/user/avatar.jpg'
	}
	return result
}


/**
 * 设置 用户信息
 */
export function setUser(user) {
	return uni.setStorageSync('user', user)
}

/**
 * 获取 token
 */
export function getToken() {
	return uni.getStorageSync('access_token')
}

/**
 * 获取 token 过期时间
 */
export function getTokenExpiredAt() {
  return wx.getStorageSync('access_expired_at')
}

/**
 * 设置 token 和 过期时间
 */
export function setToken(data) {

	const access_token = data.access_token
	const access_expored_at = new Date().getTime() + data.expired_at * 1000
	
	uni.setStorageSync('access_token', access_token)
	uni.setStorageSync('access_expired_at', access_expored_at)
}

/**
 * 退出登陆
 */
export function logout() {
	return uni.clearStorage();
}

	
